package com.wangchao.administrator.zenmechi.ui.mainactivity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.wangchao.administrator.zenmechi.R;
import com.wangchao.administrator.zenmechi.ui.fragment.buyFragment.BuyFragment;
import com.wangchao.administrator.zenmechi.ui.fragment.caipufragment.CaipuFragment;
import com.wangchao.administrator.zenmechi.ui.fragment.HomeFragment;
import com.wangchao.administrator.zenmechi.ui.fragment.shopfragment.ShopFragment;
import com.wangchao.administrator.zenmechi.customview.BadgeView;
import com.wangchao.administrator.zenmechi.ui.fragment.ShopFragment;
import com.wangchao.administrator.zenmechi.customview.MyRadioButton;
import com.wangchao.administrator.zenmechi.ui.fragment.UserFragment;
import com.wangchao.administrator.zenmechi.ui.fragment.buyFragment.BuyFragment;
import com.wangchao.administrator.zenmechi.ui.fragment.caipufragment.CaipuFragment;
import com.wangchao.administrator.zenmechi.ui.fragment.homefragment.HomeFragment;
import com.wangchao.administrator.zenmechi.ui.fragment.shopfragment.ShopFragment;
<<<<<<< HEAD
=======

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
>>>>>>> fce50b48c47bae2058634ed8df17884a408c1140

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.layout)
    RelativeLayout layout;
    @BindView(R.id.rg_main)
    RadioGroup rgMain;
    private Fragment mHomeFragment, mCaipuFragment, mShopFragment, mBuyFragment, mUserFragment,mCurrentFragment;
    @BindView(R.id.btn_msg)
    Button btnMsg;
    @BindView(R.id.rb_buy)
    MyRadioButton rbBuy;
    private Fragment mHomeFragment, mCaipuFragment, mShopFragment, mBuyFragment, mUserFragment, mCurrentFragment;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.layout, mHomeFragment).commit();
        initRadioGroup();
        setButton(btnMsg);

        EventBus.getDefault().register(this);
    }

    private void setButton(View view) {
        BadgeView badge1 = new BadgeView(this, btnMsg);// 创建一个BadgeView对象，view为你需要显示提醒的控件
        badge1.setText("12"); // 需要显示的提醒类容
        badge1.setBadgePosition(BadgeView.POSITION_TOP_LEFT);// 显示的位置.右上角,BadgeView.POSITION_BOTTOM_LEFT,下左，还有其他几个属性
        badge1.setTextColor(Color.WHITE); // 文本颜色
        badge1.setBadgeBackgroundColor(Color.RED); // 提醒信息的背景颜色，自己设置
        badge1.setTextSize(8); // 文本大小
        //badge1.setBadgeMargin(3, 3); // 水平和竖直方向的间距
        badge1.setBadgeMargin(2); //各边间隔
        // badge1.toggle(); //显示效果，如果已经显示，则影藏，如果影藏，则显示
        badge1.show();// 只有显示
        // badge1.hide();//影藏显示
    }

    private void initFragment() {
        manager = getSupportFragmentManager();
        mHomeFragment = new HomeFragment();
        mCaipuFragment = new CaipuFragment();
        mShopFragment = new ShopFragment();
        mBuyFragment = new BuyFragment();
        mUserFragment = new UserFragment();
        mCurrentFragment = mHomeFragment;
    }

    private void initRadioGroup() {
        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                switch (i) {
                    case R.id.rb_home:
                        switchFragment(mHomeFragment);
                        break;
                    case R.id.rb_caipu:
                        switchFragment(mCaipuFragment);
                        break;
                    case R.id.rb_shop:
                        switchFragment(mShopFragment);
                        break;
                    case R.id.rb_buy:
                        switchFragment(mBuyFragment);
                        break;
                    case R.id.rb_user:
                        switchFragment(mUserFragment);
                        break;
                }
            }
        });
    }

    public void switchFragment(Fragment fragment) {
        FragmentTransaction transaction = manager.beginTransaction();
        if (!fragment.isAdded()) {
            transaction.hide(mCurrentFragment).add(R.id.layout, fragment).commit();
        } else {
            transaction.hide(mCurrentFragment).show(fragment).commit();
        }
        mCurrentFragment = fragment;
    }


    public void click(View view) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(String str) {
        if ("basket".equals(str)) {
            rbBuy.setChecked(true);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
